package cn.edu.suda.catalina.utils;

import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.WebElement;

import java.util.*;

/**
 * Created by admin on 2016/12/20.
 */
public class TraversalUtils extends SessionUtils {

    public TraversalUtils(String appPath, String appName, String appPackage, String appActivity) {
        super(appPath, appName, appPackage, appActivity);
    }

    /**
     * 比较界面是否改变
     * @param list1
     * @param list2
     * @return
     */
    private boolean equals(List<String> list1, List<String> list2) {
        if (list1.size() != list2.size()) {
            return false;
        } else {
            for (int i = 0; i < list1.size(); ++i) {
                if (!list1.get(i).equals(list2.get(i))) {
                    return false;
                }
            }
            return true;
        }
    }

    private List<String> toStringList(List<WebElement> list) {
        List<String> vList = new ArrayList<>();
        for (int i = 0; i < list.size(); ++i) {
            vList.add(list.get(i).getLocation().toString());
        }
        return vList;
    }

    /**
     * 将下一个未被访问过的控件进栈
     * @param webElements
     * @param visitedUtils
     * @param activity
     */
    private boolean pushWebElement(Stack<WebElement> webElements, VisitedUtils<String, String> visitedUtils, String activity) {
        List<WebElement> webElementList = driver.findElementsByXPath("//*[@enabled='true']");
        for (WebElement webElement : webElementList) {
            System.out.println(webElement);
            if (!visitedUtils.containsV(activity, webElement.getLocation().toString())) {
                webElements.push(webElement);
                break;
            }
            if (webElement.equals(webElementList.get(webElementList.size() - 1))) {
                return false;
            }
        }
        return true;
    }

    public void traversal(String appPackage, String appActivity) {
        Stack<String> activities = new Stack<>();
        Stack<WebElement> webElements = new Stack<>();
        VisitedUtils<String, String> visitedUtils = new VisitedUtils<>();

        visitedUtils.addK(appActivity);
        activities.push(appActivity);
        while (!activities.isEmpty()) {
            String activity = activities.pop();
            ((AndroidDriver<WebElement>) driver).startActivity(appPackage, activity);
            //driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
            if (pushWebElement(webElements, visitedUtils, activity)) {
                while (!webElements.isEmpty()) {
                    WebElement webElement = webElements.pop();
                    List<String> webElementList = toStringList(driver.findElementsByXPath("//*[@enabled='true']"));//页面是否变化
                    String location = webElement.getLocation().toString();
                    visitedUtils.addV(activity, location);//添加WebElement到已访问
                    webElement.click();
                    if (activity.equals(((AndroidDriver<WebElement>) driver).currentActivity())) {
                        if (equals(webElementList, toStringList(driver.findElementsByXPath("//*[@enabled='true']")))) {
                            if (!pushWebElement(webElements, visitedUtils, activity)) {
                                break;
                            }
                        } else {
                            if (pushWebElement(webElements, visitedUtils, activity)) {
                                visitedUtils.removeV(activity, location);
                            } else {
                                activities.push(activity);
                                break;
                            }
                        }
                    } else {
                        activities.push(activity);
                        activity = ((AndroidDriver<WebElement>) driver).currentActivity();
                        if (visitedUtils.containsK(activity)) {
                            break;
                        } else {
                            visitedUtils.addK(activity);//添加Activity到已访问
                            if (!pushWebElement(webElements, visitedUtils, activity)) {
                                break;
                            }
                        }
                    }
                }//while
            }//if
        }//while
    }
}
